<?php

namespace Fn;

class State {
    //此处的州仅指美国的50个州
    /*$field 默认返回全部州,如果传参则返回对应州的键值对数组
    默认州二位码为键,若果$nameToCode为真则州全名为键*/
    static function getState($field = 'ALL',$nameToCode = false) {
        $states = array(
            'AL' => 'Alabama',
            'AK' => 'Alaska',
            'AZ' => 'Arizona',
            'AR' => 'Arkansas',
            'CA' => 'California',
            'CO' => 'Colorado',
            'CT' => 'Connecticut',
            'DE' => 'Delaware',
            'FL' => 'Florida',
            'GA' => 'Georgia',
            'HI' => 'Hawaii',
            'ID' => 'Idaho',
            'IL' => 'Illinois',
            'IN' => 'Indiana',
            'IA' => 'Iowa',
            'KS' => 'Kansas',
            'KY' => 'Kentucky',
            'LA' => 'Louisiana',
            'ME' => 'Maine',
            'MD' => 'Maryland',
            'MA' => 'Massachusetts',
            'MI' => 'Michigan',
            'MN' => 'Minnesota',
            'MS' => 'Mississippi',
            'MO' => 'Missouri',
            'MT' => 'Montana',
            'NE' => 'Nebraska',
            'NV' => 'Nevada',
            'NH' => 'New Hampshire',
            'NJ' => 'New Jersey',
            'NM' => 'New Mexico',
            'NY' => 'New York',
            'NC' => 'North Carolina',
            'ND' => 'North Dakota',
            'OH' => 'Ohio',
            'OK' => 'Oklahoma',
            'OR' => 'Oregon',
            'PA' => 'Pennsylvania',
            'RI' => 'Rhode Island',
            'SC' => 'South Carolina',
            'SD' => 'South Dakota',
            'TN' => 'Tennessee',
            'TX' => 'Texas',
            'UT' => 'Utah',
            'VT' => 'Vermont',
            'VA' => 'Virginia',
            'WA' => 'Washington',
            'WV' => 'West Virginia',
            'WI' => 'Wisconsin',
            'WY' => 'Wyoming',
        );
        $field = trim(strtoupper($field));
        if($nameToCode){
           $states = array_flip($states);
           $field = ucwords($field);
        }
        if ($field === 'ALL') {
            return $states;
        }else{
            return array($field => $states[$field]);
        }
    }

    //根据州名返回州2位code
    static function getCodeByName($name){
        $name = ucwords($name);
        $states = self::getState($name,true);
        return $states[$name]?$states[$name]:'';
    }

    //根据州2位code返回州名
    static function getNameByCode($code){
        $code = strtoupper($code);
        $states = self::getState($code);
        return $states[$code]?$states[$code]:'';
    }
}